<?php

declare(strict_types=1);

namespace App\Service;

use App\Model\Company as CompanyModel;
use App\Model\CompanyType as CompanyTypeModel;

class Company extends BaseService
{
    // 分页列表
    public function list(array $map, int $page = 10)
    {
        return CompanyModel::query()->with(['type:company_type_name,key'])->select('company_name','company_type','is_phone_order','status','created_at')->where($map)->orderBy('company_id', 'desc')->paginate($page);
    }

    // 分页列表
    public function allList(array $map)
    {
        return CompanyModel::query()->select('company_name as label','company_id as value')->where($map)->orderBy('company_id', 'desc')->get();
    }

    // 添加
    public function addCompany($data)
    {
        unset($data['company_id']);
        return CompanyModel::insert($data);
    }

    // 编辑
    public function editCompany($company_id, $data)
    {
        print_r($data);
        return CompanyModel::query()->where('company_id', $company_id)->update($data);
    }

    // 删除
    public function deleteCompany($company_id)
    {
        return CompanyModel::where('company_id', $company_id)->delete();
    }

    // 获取公司类型
    public function getCompanyType()
    {
        return CompanyTypeModel::select('company_type_name as label', 'key as value')->get();
    }

    // 添加公司类型
    public function addCompanyType($data)
    {
        unset($data['id']);
        return CompanyTypeModel::insert($data);
    }

    // 编辑公司类型
    public function editCompanyType($id, $data)
    {
        return CompanyTypeModel::query()->where('id', $id)->update($data);
    }
}
